package com.example.huhaichao.music.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.example.huhaichao.music.MainActivity;
import com.example.huhaichao.music.util.MusicUtils;

public class MusicService extends Service {
    public MediaPlayer mediaPlayer;
    public boolean tag = false;
    private int currentPosition = 0;// 设置默认进度条当前位置
    private int serviceMark;
    private MainActivity mainActivity;
    public MusicService() {
        mediaPlayer = new MediaPlayer();
    }

    private String UrlPlaying;
    //  通过 Binder 来保持 Activity 和 Service 的通信
    public MyBinder binder = new MyBinder();

    public class MyBinder extends Binder {
        public MusicService getService() {
            return MusicService.this;
        }
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    public void setMainActivity(MainActivity activity)
    {
        this.mainActivity=activity;
    }

    //播放音乐
    public void playMusic(int position, int mark) {
        serviceMark = mark;
        Log.d("MusicService", "serviceMark=" + serviceMark);
        try {
            mediaPlayer.reset();
            if (serviceMark == 0)//fragment1
                UrlPlaying = MusicUtils.aMusicList.get(position);
            else if (serviceMark == 1)//fragment2
                UrlPlaying = MusicUtils.mMusicList.get(position);
            else if(serviceMark==2)//fragment3
                UrlPlaying = MusicUtils.sMusicList.get(position);

            mediaPlayer.setDataSource(UrlPlaying);
            mediaPlayer.prepare();
            mediaPlayer.start();
            mediaPlayer.setLooping(true);// 设置循环播放
            mediaPlayer.seekTo(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //暂停和开始
    public void playOrPause() {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
        } else {
            mediaPlayer.start();
        }
    }

    //下一首
    public void nextMusic(int position) {
        mediaPlayer.stop();
        if (serviceMark == 0) {
            if ((position+1) > (MusicUtils.aMusicList.size() - 1)) {
                Toast.makeText(this, "没有下一首", Toast.LENGTH_SHORT).show();

            } else {
                playMusic((position+1), serviceMark);
            }
        }
        if (serviceMark == 1) {
            if ((position+1) > (MusicUtils.mMusicList.size() - 1)) {
                Toast.makeText(this, "没有下一首", Toast.LENGTH_SHORT).show();
                //mainActivity.resetUI();
            } else {
                playMusic((position+1), serviceMark);
            }
        }
        if (serviceMark == 2) {
            if ((position+1) > (MusicUtils.sMusicList.size() - 1)) {
                Toast.makeText(this, "没有下一首", Toast.LENGTH_SHORT).show();
                //mainActivity.resetUI();
            } else {
                playMusic((position+1), serviceMark);
            }
        }
    }

    //上一首
    public void preMusic(int position) {
        mediaPlayer.stop();
        if ((position-1) < 0) {
            Toast.makeText(this, "没有上一首", Toast.LENGTH_SHORT).show();

            mainActivity.resetUI();
        } else {
            playMusic((position-1), serviceMark);
        }
    }

    //得到当前播放位置
    public int getCurrentPosition() {
        if (mediaPlayer != null) {
            int total = mediaPlayer.getDuration();// 总时长
            if (currentPosition < total) {
                currentPosition = mediaPlayer.getCurrentPosition();
            }
        }
        return currentPosition;
    }

    //返回播放的路径
    public String getUrlPlaying() {
        return UrlPlaying;
    }
}

